// pages/car/detail.js
var WxParse = require('../../wxParse/wxParse.js');
import {
    platform,
    wxapp,
    user
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: '',
        show: false,
        appointment_time:'',
        minDate: new Date().getTime() + 24*60*60*1000,
        guide_id:'',
        contact_name:'',
        contact_phone:'',
        note:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            guide_id:options.id
        })
        platform.guide.detail({
            guide_id: options.id
        }, res => {
            if (res.code == 1) {
                this.setData({
                    info: res.data
                })
                WxParse.wxParse('bindName', 'html', res.data.intro_content, this, 5)

            }
        })
    },
    onDisplay() {
        this.setData({ show: true });
      },
    onClose() {
        this.setData({
            show: false
        });
    },
    formatDate(date) {
        date = new Date(date);
        console.log(date)
        return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    },
    onConfirm(event) {
        this.setData({
            show: false,
            appointment_time: this.formatDate(event.detail),
        });
    },
    // 下单
    toSubmit() {
        let that = this
        let obj = {
            guide_id:this.data.guide_id,
            appointment_time:this.data.appointment_time,
            contact_name:this.data.contact_name,
            contact_phone:this.data.contact_phone,
            note:this.data.note
        }
        console.log(obj)
        if(obj.appointment_time==''){
            wx.showToast({
              title: '请选择预约日期',
              icon:'none'
            })
            return false
        }
        if(obj.contact_name==''){
            wx.showToast({
              title: '请输入联系人姓名',
              icon:'none'
            })
            return false
        }
        if(obj.contact_phone==''){
            wx.showToast({
              title: '请输入联系电话',
              icon:'none'
            })
            return false
        }
        platform.guide.orderSubmit(obj, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg,
                })
                that.setData({
                    order_id: res.data.id,
                    order_no: res.data.order_no,
                })
                that.toPay()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
  // 支付
  toPay() {
    // hotel.order.pay({

    // })
    let order_no = this.data.order_no
    wx.login({
        success(res) {
            var code = res.code
            wx.showLoading({
                title: "请稍后...",
                mask: true
            })
            platform.guide.orderPay({
                order_no,
                code
            }, res => {
                let ret_code = res.code
                let ret_msg = res.msg
                if (ret_code != 1) {
                    //   Toast(ret_msg)
                    wx.showToast({
                        title: ret_msg,
                        icon: 'error'
                    })
                    return
                }
                let {
                    nonceStr,
                    paySign,
                    signType,
                    timeStamp
                } = res.data
                wx.requestPayment({
                    nonceStr,
                    package: res.data.package,
                    paySign,
                    timeStamp,
                    signType,
                    success(res) {
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 1500
                        });
                        setTimeout(() => {
                            wx.redirectTo({
                              url: '/pages/order/index?active=3',
                            })
                        }, 1000)
                    },
                    fail(res) {
                        wx.showToast({
                            title: '支付失败',
                            duration: 1500,
                            icon: 'error'
                        });
                        setTimeout(() => {
                            wx.redirectTo({
                                url: '/pages/order/index?active=3',
                              })
                        }, 1000)

                    }
                })
            })
        }
    })
},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo()

    },
    getInfo() {
        let token = wx.getStorageSync('token')
        if (token) {
            // 获取用户详情
            user.detailS({}, res => {
                if(res.code==20003){
					this.getUserInfos()
                }else{
                   
                }
                
            })
        } else {
			this.getUserInfos()

        }
    },
 	/**
	 * 授权登录
	 */
	getUserInfos(e) {
        let that = this
		wx.login({
			success(res) {
				console.log(res)
				let code = res.code
				wx.getUserInfo({
					success: (res) => {
						var encryptedData = res.encryptedData;
						var iv = res.iv
						var info = res.rawData
						var obj = {
							code: code,
							iv: iv,
							encryptedData: encryptedData
						}
						wxapp.login(obj, res => {
							if (res.code == 1) {
								wx.setStorage({
									key: "token",
									data: res.data.token
                                })
							} else {
								wx.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					},
					fail: res => {

					}
				})
			}
		})
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})